package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.OrderProductCheckPriceEntity;
import com.zhenwei.zscm.mapper.IOrderProductCheckPriceMapper;
import com.zhenwei.zscm.mappings.OrderProductCheckPriceMapping;
import com.zhenwei.zscm.model.dto.order.OrderProductCheckPriceDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author DuckyYang
* @description 针对表【order_product_check_price】的数据库操作Service实现
* @createDate 2025-05-22 09:40:57
*/
@Service
public class OrderProductCheckPriceRepo extends ServiceImpl<IOrderProductCheckPriceMapper, OrderProductCheckPriceEntity> {

    /**
     * 添加工单产品配件费用清单
     * @param prices 费用清单数据
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean createCheckPrice(List<OrderProductCheckPriceDTO> prices) {
        List<OrderProductCheckPriceEntity> entityList = Linq.mapList(prices, OrderProductCheckPriceMapping.INSTANCE::dto2Entity);
        entityList.forEach(e -> e.setGuid(IdUtil.simpleUUID()));
        return this.saveBatch(entityList);
    }

    /**
     * 删除工单产品检测配件费用数据
     * @param orderId 工单id
     * @param productSn 产品sn
     * @return bool
     */
    public boolean delete(String orderId, String productSn) {
        var u = new LambdaUpdateWrapper<OrderProductCheckPriceEntity>()
                .eq(OrderProductCheckPriceEntity::getOrderId, orderId)
                .eq(OrderProductCheckPriceEntity::getProductSn, productSn)
                .eq(OrderProductCheckPriceEntity::getDeleted, false)
                .set(OrderProductCheckPriceEntity::getDeleted, true);
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }
    /**
     * 删除工单所有产品检测配件费用数据
     * @param orderId 工单id
     * @return bool
     */
    public boolean delete(String orderId) {
        var u = new LambdaUpdateWrapper<OrderProductCheckPriceEntity>()
                .eq(OrderProductCheckPriceEntity::getOrderId, orderId)
                .eq(OrderProductCheckPriceEntity::getDeleted, false)
                .set(OrderProductCheckPriceEntity::getDeleted, true);
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }
    /**
     * 查询工单产品检测配件费用清单
     * @param orderId 工单id
     * @param productSn 产品sn
     * @return List<OrderProductCheckPriceDTO>
     */
    public List<OrderProductCheckPriceDTO> list(String orderId, String productSn) {
        var q = new LambdaQueryWrapper<OrderProductCheckPriceEntity>()
                .eq(OrderProductCheckPriceEntity::getOrderId, orderId)
                .eq(OrderProductCheckPriceEntity::getProductSn, productSn)
                .eq(OrderProductCheckPriceEntity::getDeleted, false)
                .orderByAsc(OrderProductCheckPriceEntity::getId);
        var list = this.list(q);
        return Linq.mapList(list, OrderProductCheckPriceMapping.INSTANCE::entity2Dto);
    }
    /**
     * 查询工单产品检测配件费用清单
     * @param orderId 工单id
     * @return List<OrderProductCheckPriceDTO>
     */
    public List<OrderProductCheckPriceDTO> list(String orderId) {
        var q = new LambdaQueryWrapper<OrderProductCheckPriceEntity>()
                .eq(OrderProductCheckPriceEntity::getOrderId, orderId)
                .eq(OrderProductCheckPriceEntity::getDeleted, false)
                .orderByAsc(OrderProductCheckPriceEntity::getId);
        var list = this.list(q);
        return Linq.mapList(list, OrderProductCheckPriceMapping.INSTANCE::entity2Dto);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<OrderProductCheckPriceDTO> orderProductCheckPriceList) {
        var entityList = Linq.mapList(orderProductCheckPriceList, OrderProductCheckPriceMapping.INSTANCE::dto2Entity);
        return this.saveBatch(entityList);
    }
}




